#include "clock.h"
#include <signal.h>
#include <sys/time.h>
#include <stdlib.h>
#include <stdio.h>

void clockTicksInit(void(*handler) (int))
{
  //Mise en place du gestionaire de signaux
  struct sigaction act;
  sigemptyset(&act.sa_mask);
  act.sa_handler = handler;
  act.sa_flags = 0;
  sigaction(SIGVTALRM,&act,NULL);
}

void clockTicksStart(int priority)
{
  //Mise en place du timer
  struct itimerval clockTicks;
  //Période du signal
  clockTicks.it_interval.tv_sec = 0;
  clockTicks.it_interval.tv_usec = 0;
  
  //Premier bip
  clockTicks.it_value.tv_sec = 0;
  clockTicks.it_value.tv_usec = SIZE_TIME_SLICE_MIN  + (SIZE_TIME_SLICE_FACT*(priority-PRIORITY_MIN));
 setitimer(ITIMER_VIRTUAL, &clockTicks, NULL);

}

void clockTicksStop()
{
  //arret du timer
  struct itimerval clockTicks;
  clockTicks.it_interval.tv_sec = 0;
  clockTicks.it_interval.tv_usec = 0;
  clockTicks.it_value.tv_sec = 0;
  clockTicks.it_value.tv_usec = 0;
  setitimer(ITIMER_VIRTUAL, &clockTicks, NULL);

}
